package cn.qzuie.smart.module.dorm.controller.admin.billpay;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.CommonResult;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;
import static cn.qzuie.smart.framework.common.pojo.CommonResult.success;

import cn.qzuie.smart.framework.excel.core.util.ExcelUtils;

import cn.qzuie.smart.framework.apilog.core.annotation.ApiAccessLog;
import static cn.qzuie.smart.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.qzuie.smart.module.dorm.controller.admin.billpay.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.billpay.BillPayDO;
import cn.qzuie.smart.module.dorm.service.billpay.BillPayService;

@Tag(name = "管理后台 - 账单支付")
@RestController
@RequestMapping("/dorm/bill-pay")
@Validated
public class BillPayController {

    @Resource
    private BillPayService billPayService;

    @PostMapping("/create")
    @Operation(summary = "创建账单支付")
    @PreAuthorize("@ss.hasPermission('dorm:bill-pay:create')")
    public CommonResult<Long> createBillPay(@Valid @RequestBody BillPaySaveReqVO createReqVO) {
        return success(billPayService.createBillPay(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账单支付")
    @PreAuthorize("@ss.hasPermission('dorm:bill-pay:update')")
    public CommonResult<Boolean> updateBillPay(@Valid @RequestBody BillPaySaveReqVO updateReqVO) {
        billPayService.updateBillPay(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账单支付")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dorm:bill-pay:delete')")
    public CommonResult<Boolean> deleteBillPay(@RequestParam("id") Long id) {
        billPayService.deleteBillPay(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除账单支付")
                @PreAuthorize("@ss.hasPermission('dorm:bill-pay:delete')")
    public CommonResult<Boolean> deleteBillPayList(@RequestParam("ids") List<Long> ids) {
        billPayService.deleteBillPayListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账单支付")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dorm:bill-pay:query')")
    public CommonResult<BillPayRespVO> getBillPay(@RequestParam("id") Long id) {
        BillPayDO billPay = billPayService.getBillPay(id);
        return success(BeanUtils.toBean(billPay, BillPayRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账单支付分页")
    @PreAuthorize("@ss.hasPermission('dorm:bill-pay:query')")
    public CommonResult<PageResult<BillPayRespVO>> getBillPayPage(@Valid BillPayPageReqVO pageReqVO) {
        PageResult<BillPayDO> pageResult = billPayService.getBillPayPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillPayRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出账单支付 Excel")
    @PreAuthorize("@ss.hasPermission('dorm:bill-pay:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBillPayExcel(@Valid BillPayPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BillPayDO> list = billPayService.getBillPayPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "账单支付.xls", "数据", BillPayRespVO.class,
                        BeanUtils.toBean(list, BillPayRespVO.class));
    }

}